package coupon_distribution

import (
	sqlc "bp/db/sqlc"
	"bp/internal/render"
	// sharedConv "bp/internal/rpcserver/shared/conv"
	sharedVld "bp/internal/rpcserver/shared/vld"
	"bp/internal/service/entity/coupon"
	"bp/internal/util"
	couponDistributionGrpc "bp/rpc/gen/webapi/coupon_distribution"
	"context"
)

// VldGetCouponDistributionReq 验证获取优惠券投放请求
func (s *CouponDistributionWebApiImp) VldGetCouponDistributionReq(ctx context.Context, q *sqlc.Queries, req *couponDistributionGrpc.GetCouponDistributionReq) (distributionID int64, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	if req.Id < 0 {
		err = render.InvalidArgumentRange("id")
		return
	}
	distributionID = req.Id
	slog.Infof("coupon_distribution_imp/query_vld.VldGetCouponDistributionReq ok")
	return
}

// VldListCouponDistributionsReq 验证列出优惠券投放请求
func (s *CouponDistributionWebApiImp) VldListCouponDistributionsReq(ctx context.Context, q *sqlc.Queries, req *couponDistributionGrpc.ListCouponDistributionsReq) (ptr *coupon.ListCouponDistributionPtrsParam, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	if req.CouponId < 0 {
		err = render.InvalidArgumentRange("coupon_id")
		return
	}
	if !sharedVld.IsIssuerArray(req.Issuers, true) {
		err = render.InvalidArgumentValue("issuers")
		return
	}
	if !sharedVld.IsCouponDistributeSceneArray(req.DistributeScenes, true) {
		err = render.InvalidArgumentValue("distribute_scenes")
		return
	}
	ptr = &coupon.ListCouponDistributionPtrsParam{
		CouponID:         req.CouponId,
		Issuers:          req.Issuers,
		DistributeScenes: req.DistributeScenes,
	}
	slog.Infof("coupon_distribution_imp/query_vld.VldListCouponDistributionsReq ok")
	return
}

// VldListCouponsReq 验证列出优惠券投放及其统计请求
func (s *CouponDistributionWebApiImp) VldListCouponDistributionWithStatsReq(ctx context.Context, q *sqlc.Queries, req *couponDistributionGrpc.ListCouponDistributionWithStatsReq) (ptr *coupon.ListCouponDistributionWithStatPtrsParam, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	if req.CouponId < 0 {
		err = render.InvalidArgumentRange("coupon_id")
		return
	}
	if !sharedVld.IsIssuerArray(req.Issuers, true) {
		err = render.InvalidArgumentValue("issuers")
		return
	}
	if !sharedVld.IsCouponDistributeSceneArray(req.DistributeScenes, true) {
		err = render.InvalidArgumentValue("distribute_scenes")
		return
	}
	ptr = &coupon.ListCouponDistributionWithStatPtrsParam{
		CouponID:         req.CouponId,
		Issuers:          req.Issuers,
		DistributeScenes: req.DistributeScenes,
	}
	slog.Infof("coupon_distribution_imp/query_vld.VldListCouponWithStatsReq ok")
	return
}
